# coding=UTF-8
from model import Partido, Equipo, Evento
import Config
import xml.dom.minidom as minidom

class LiveXmlParser:
	def live2Partido(xml, partido):
		doc = minidom.parseString(xml)
		hometeam 	= doc.getElementsByTagName('HomeTeamName')[0].firstChild.nodeValue
		awayteam 	= doc.getElementsByTagName('AwayTeamName')[0].firstChild.nodeValue
		homegoals 	= doc.getElementsByTagName('HomeGoals')[0].firstChild.nodeValue
		awaygoals 	= doc.getElementsByTagName('AwayGoals')[0].firstChild.nodeValue
		hometeamid 	= doc.getElementsByTagName('HomeTeamID')[0].firstChild.nodeValue
		awayteamid 	= doc.getElementsByTagName('AwayTeamID')[0].firstChild.nodeValue
		#	self, local, goleslocal, golesvisitante, visitante, minuto, partidoliga=True, jornada=None, grupo=None, eventos=None
		if partido == None:
			local = Equipo(hometeamid, hometeam)
			visitante = Equipo(awayteamid, awayteam)
			partido = Partido(local, homegoals, awaygoals, visitante, 0)
		#self, id, nombre, nombrecorto="nombrecorto", pj=-1, g=-1, e=-1, p=-1, gf=-1, gc=-1, avg=-1, ptos=-1, grupo=-1
		
		eventos = []
		eventosXml = doc.getElementsByTagName('Event')
		for e in eventosXml:
			minute =			e.getElementsByTagName('Minute')[0].firstChild.nodeValue
			subjectplayerid = 	e.getElementsByTagName('SubjectPlayerID')[0].firstChild.nodeValue
			subjectteamid =		e.getElementsByTagName('SubjectTeamID')[0].firstChild.nodeValue
			objectplayerid =	e.getElementsByTagName('ObjectPlayerID')[0].firstChild.nodeValue
			eventkey =			e.getElementsByTagName('EventKey')[0].firstChild.nodeValue
			eventtext =			e.getElementsByTagName('EventText')[0].firstChild.nodeValue
			#self, eventKey=None, minute=None, subjectPlayerId=None, subjectTeamId=None, objectPlayerId=None, eventText=None
			evento = Evento(eventkey, minute, subjectplayerid, subjectteamid, objectplayerid, eventtext)
			eventos.append(evento)
		partido.eventos = eventos
		return partido
	live2Partido = staticmethod(live2Partido)
	